package com.example.abe.diandi.user.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.example.BindView;
import com.example.OnClick;
import com.example.abe.diandi.R;
import com.example.abe.diandi.base.BaseActivity;
import com.example.abe.diandi.main.AppConfig;
import com.example.abe.diandi.nohttp.CallServer;
import com.example.abe.diandi.nohttp.HttpListener;
import com.example.abe.diandi.nohttp.ApiUtil;
import com.example.abe.diandi.user.model.UserModel;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import example.abe.com.framework.util.ToastUtil;

import static com.example.abe.diandi.nohttp.ApiUtil.METHOD;
import static com.example.abe.diandi.nohttp.ApiUtil.M_USER_REGISTER;
import static com.example.abe.diandi.nohttp.ApiUtil.SERVICE;
import static com.example.abe.diandi.nohttp.ApiUtil.S_USER;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.act_register_tv_register)
    protected TextView mTvRegister;

    @BindView(R.id.act_login_et_username)
    protected EditText mEtUsername;

    @BindView(R.id.act_register_et_password)
    protected EditText mEtPassword;

    private String mStrUsername;
    private String mStrPassword;

    @Override
    public int getLayoutID() {
        return R.layout.activity_register;
    }

    @Override
    public void initData() {
    }

    @Override
    public void initView() {
        mEtUsername.addTextChangedListener(mEtWatcher);
        mEtPassword.addTextChangedListener(mEtWatcher);
    }

    @Override
    public void onSideSlipEvent(){
        finish();
    }

    TextWatcher mEtWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            mStrUsername = mEtUsername.getText().toString();
            mStrPassword = mEtPassword.getText().toString();
        }
    };

    @OnClick({R.id.act_register_tv_register})
    protected void registerAction() {
        Request<String> request = NoHttp.createStringRequest(ApiUtil.URL, RequestMethod.POST);
        request.add(SERVICE, S_USER)
                .add(METHOD, M_USER_REGISTER)
                .add("name", mStrUsername)
                .add("password", mStrPassword);
        CallServer.getInstance().add(this, request, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {
                JsonObject jsonObj = new JsonParser().parse(response.get()).getAsJsonObject();
                Boolean isSuccess = jsonObj.get("success").getAsBoolean();
                if (isSuccess) {
                    JsonObject data = jsonObj.getAsJsonObject("data");
                    UserModel model = new Gson().fromJson(data, UserModel.class);
                    AppConfig.setUserModel(model);
                    ToastUtil.makeText("注册成功");
                    finish();
                } else {
                    ToastUtil.makeText("注册失败");
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
                ToastUtil.makeText("注册失败");
            }
        });
    }
}
